<include file="Public/min-header"/>
<div class="wrapper">
	<style type="text/css">
		#origin .yincang{display: nones;}
		#tishi{display: none;}
		#origin #tishi{display: block;}
		.col-xs-10{width: 90%;}
	</style>
  <include file="Public/breadcrumb"/>
    <section class="content">
        <!-- Main content --> 
        <div class="container-fluid">
            <div class="pull-right">
                <a href="javascript:history.go(-1)" data-toggle="tooltip" title="" class="btn btn-default" data-original-title="返回"><i class="fa fa-reply"></i></a>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="fa fa-list"></i> 拆分订单</h3>
                </div>
                <div class="panel-body">
                    <!--表单数据-->
                    <form method="post" action="{:U('Order/wujingsplit_order')}" id="split_order">
                        <div class="tab-pane">
                            <table class="table table-bordered">
                                <tbody>
								<tr style="color:#3c8dbc;"><td>单号:</td>
									<td>
										<div class="col-xs-9">
										{$order.order_sn}
										</div></td>
								</tr>
                                <tr><td>费用信息:</td>
                                	<td>
                                		<div class="col-xs-9">
                                		<input type="hidden" name="order_id" value="{$order.order_id}">
                                		 商品总价：{$order.goods_price}+运费:{$order.shipping_price}-积分：{$order.integral}-优惠:{$order.discount}                                        
                                		</div>
                                	</td>
                                </tr>
                                <tr>
                                    <td>买家:</td>
                                    <td>
                                    <div class="form-group">
	                                    <div class="col-xs-2" style="line-height:45px"> {$order.consignee}</div>
                                        <div class="col-xs-1" style="line-height:45px">手机：</div>
                                        <div class="col-xs-2" style="line-height:45px">{$order.mobile}</div>
                                        <div class="col-xs-5"><p class="text-warning" style="color: red;font-weight: bold;">警告：拆单前必须和买家沟通确认，库存不足未经买家确认私自拆单！损害买家利益，平台将对您做出相应处罚！</p></div>
                                    </div>    
                                    </td>
                                </tr>
                                <tr id="last_tr_old">
                                    <td>原单商品列表:</td>
                                    <td id="origin" style="border:2px orange solid;"> 
									<span style="color:orange;" id="tishi">拆单规则：没货放原单 有货放新单。原单必须最少保留一个产品 <b>不能全数发货</b>。</span>
                                       <div class="form-group">
                                       		<div class="col-xs-10">
	                                       		<table class="table table-bordered">
	                                       			<thead>
	                                       			<tr>
										                <td class="text-left">商品名称</td>
										                <td class="text-left">规格</td>
										                <td class="text-left">价格</td>
										                <td class="text-left">原购数</td>								                
										                <td class="text-left">数量</td>
										                <td class="text-left">操作</td>
										            </tr>
										            </thead>
										            <tbody>
										            <foreach name="orderGoods" item="vo">
										            	<tr>
										                <td class="text-left">{$vo.goods_name}</td>            
										                <td class="text-left">{$vo.spec_key_name}</td>
										                <td class="text-left">{$vo.goods_price}</td>
										                <td class="text-left">{$vo.goods_num}</td>
										                <td class="text-left">
										                	<input type="text" id="olds_{$vo.rec_id}" rel="{$vo.rec_id}" rev="{$vo.goods_num}" name="old_goods[{$vo.rec_id}]" rel="{$vo.rec_id}" readonly="readonly" class="input-sm news" style="width:60px;" value="{$vo.goods_num}" onmouseover="layer.tips('拆单禁止修改数量!',this,{tips: [1, '#000']});">
										               	</td>
										                <td class="text-left">
										                	<a class="yincang" id="shanchu" href="javascript:void(0)"><i class="fa fa-trash-o red"> 有货</i></a>
										                </td>
										           		</tr>
										           </foreach>
										           </tbody>
	                                       		</table>
                                       	   </div>
                                       </div>                                       
                                    </td>
                                </tr>                               
                                <tr id="new_1" class="new_split"><td style="color:#00a65a">新单商品列表:</td><td style="border:3px #00a65a solid"> 
                                									<span style="color:orange;" id="tishi">拆单规则：没货放原单 没货放新单。原单必须最少保留一个产品 <b>不能全数发货</b>。</span>
                                                                       <div class="form-group">
                                                                       		<div class="col-xs-10">
                                	                                       		<table class="table table-bordered">
                                	                                       			<thead>
                                	                                       			<tr>
                                										                <td class="text-left">商品名称</td>
                                										                <td class="text-left">规格</td>
                                										                <td class="text-left">价格</td>
                                										                <td class="text-left">原购数</td>
                                										                <td class="text-left">数量</td>
                                										                <td class="text-left">操作</td>
                                										            </tr>
                                										            </thead>
                                										            <tbody>
                                										          </tbody>
                                	                                       		</table>
                                                                       	   </div>
                                                                       </div>                                       
                                                                    </td></tr>
								<tr id="last_tr" style="display: none;">
                                    <td>管理员备注:</td>
                                    <td>
                                    <div class="form-group ">
	                                    <div class="col-xs-4">
                                        	<textarea style="width:450px; height:100px;" name="admin_note">商家拆单。</textarea>
                                        </div>
                                    </div>    
                                    </td>
                                </tr>
                             </tbody>
                          </table>
                          <div class="col-xs-12">
                          	<div class="pull-left">
                          		<p class="text-danger" id="error_log"></p>
                          	</div>
	                        <div class="pull-right">
								<button type="button" class="btn btn-default pull-left" onclick="window.location.reload();" style="margin-right: 28px;">重置</button>
		                        <button class="btn btn-info" type="button" onclick="checkSubmit()">
		                            <i class="ace-icon fa fa-check bigger-110"></i>保存
		                        </button>
	                        </div>
                        </div>
                      </div>
                    </form> 
                </div>
            </div>
        </div> 
    </section>
</div>
<script>
var no = 1;
$(function(){
	//add_split();
	$(document).on("input",".news",function(){
		var myrel=$(this).attr('rel');
		var new_zhi=$(this).val();
		var old_zhi=$('#olds_'+myrel).attr('rev');
		$('#olds_'+myrel).val(old_zhi-new_zhi);
	});
	
	$(document).on("click","#shanchu",function(){
		var pd=$(this).parent().parent().parent().parent().parent().parent().parent().parent().attr('id');
		$(this).parent().parent().remove();
		var inputs=myhtml=''; 

		if(pd=='last_tr_old'){ //老单删除
			inputs=$(this).parent().parent().find('input');
			inputs.attr('name','1_old_goods['+inputs.attr('rel')+']');
			myhtml="<tr>"+$(this).parent().parent().html()+"</tr>";
			
			myhtml=myhtml.replace(/有货/g,'没货');
			if($("#new_1 #olds_"+inputs.attr('rel')).length >= 1 ) {
				$("#new_1 #olds_"+inputs.attr('rel')).val($("#new_1 #olds_"+inputs.attr('rel')).attr('rev')); //赋值
			}else{
				$("#new_1 tbody").append(myhtml);
				
			}
		}else{ //新单删除
			inputs=$(this).parent().parent().find('input');
			inputs.attr('name','old_goods['+inputs.attr('rel')+']');
			myhtml="<tr>"+$(this).parent().parent().html()+"</tr>";
			
			myhtml=myhtml.replace(/没货/g,'有货');
			if($("#last_tr_old #olds_"+inputs.attr('rel')).length < 1 ) {
				$("#last_tr_old tbody").append(myhtml);
				inputs.attr('name','old_goods['+inputs.attr('rel')+']');
			}else{
				$("#last_tr_old #olds_"+inputs.attr('rel')).val($("#last_tr_old #olds_"+inputs.attr('rel')).attr('rev')); //赋值
			}
		}
	});
});

// function add_split(){
// 	var new_order = '';
// 	var olds_html= $('#origin').html();
// 	olds_html=olds_html.replace(/disabled="disabled"/g,'');
// 	olds_html=olds_html.replace(/有货/g,'没货');
	
// 	new_order += '<tr id="new_'+no+'" class="new_split"><td style="color:#00a65a">新单商品列表:</td><td style="border:3px #00a65a solid">'
// 	new_order +=olds_html;
// 	new_order += '</td></tr>';
// 	$('#last_tr').before(new_order);
// 	$('#new_'+no+' .input-sm').each(function(i,o){
// 		var name = $(this).attr('name');
// 		$(this).attr('name',no+'_'+name);
// 		var zhi=$(this).val();
// 		if(zhi>1){
// 			$(this).val(1);
// 		}
// 	});
// 	$('#last_tr_old .input-sm').each(function(i,o){
// 		var zhi=$(this).val();
// 		if(zhi<=1){
// 			$(this).parent().parent().remove();
// 		}else{
// 			$(this).val(zhi-1);
// 		}
// 	});
// 	no++;
// }

var b = {$goods_num_arr};

function checkSubmit(){
	var a = [],g = [];
	//$(document).on("each",'input[name*=old_goods]',function(){
	$('input[name*=old_goods]').each(function(i,o){
		var rec_id = $(o).attr('rel');
		if(!a[rec_id]){
			a[rec_id] = 0;
		}
		a[rec_id] = a[rec_id] + parseInt($(o).val());
	});
	//$(document).on("each",'#origin .input-sm',function(){
	$('#origin .input-sm').each(function(){
		g.push($(this).val());
	});

	if($('.new_split').length == 0){
		$('#error_log').html('请至少拆分一单');
		return false;
	}
	if(g.length == 0){
		$('#error_log').html('原单商品不可全部移除');
		return false;
	}
	
	for(var k in b){
		//console.log(b[k]['goods_num']);
		if(a[k] > parseFloat(b[k]['goods_num'])){
			var lt = a[k] - parseFloat(b[k]['goods_num']);
			$('#error_log').html(b[k]['goods_name']+',数量>=原商单购买数'+lt+'件');
			return false;
		}
		// if(a[k] < parseFloat(b[k]['goods_num'])){
		// 	var lt = parseFloat(b[k]['goods_num']) - a[k];
		// 	$('#error_log').empty().html(b[k]['goods_name']+',数量少于原商单购买数'+lt+'件');
		// 	return false;
		// }else{
		// 	$('#error_log').empty();
		// }
	}
	console.log(b);
	layer.confirm('拆单后，标签和单据需要重新打印！', {
	  title:'系统提示：', 
	  btn: ['知道了','取消'] //按钮
	}, function(){
	  $('#split_order').submit();
	});
}
</script>
</body>
</html>